package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private MySQLiteOpenHelper openHelper;
    private EditText etName;
    private Button btnDel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();

        openHelper = new MySQLiteOpenHelper(this);
        StudentBean studentBean = new StudentBean();
        for (int i=0;i<10;i++){
            studentBean.setName("L" + i);
            studentBean.setGender("male");
            studentBean.setNumber(String.valueOf(i));
            studentBean.setScore(String.valueOf(i+ 10));
            openHelper.insertData(studentBean);
        }

        btnDel.setOnClickListener(v -> {
            int i = openHelper.deleteDataFromDbByName(etName.getText().toString().trim());
            if (i != -1){
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        etName = findViewById(R.id.et_name);
        btnDel = findViewById(R.id.btn_del);
    }
}


